#ifndef _LIB_UTILITY
#define _LIB_UTILITY
#include "page.module.js"
#include "console.module.js"
#include "usolib.lib.js"

var Utility = {
	make_number: function(string) {
		var number = 0,
			number_string = '',
			character = '';

		for (var i = 0; character = string[ i++ ]; ) {
			number = parseInt(character, 0);

			if ( number > -1 )
				number_string += number;
		}
		return parseInt(number_string, 0);
	},
	a_timer: function(details) {
		this.fn = details.fn || function() {};
		if (details.time == 0) {
			this.time = 0;
		} else {
			this.time = details.time || 5;
		}
		this.message = details.message || false;
		this.stopping = false;

		this.stop = function() {
			this.stopping = true;
			return;
		}

		this.start = function() {
			if (this.stopping) {
				this.stopping = false;
				return;
			}
			this.time = parseInt(this.time, 0);
			if (this.message) {
				$('#mwta_close')[0].value = this.message.replace('[time]', this.time);
			}
			if (this.time > 0) {
				this.time = this.time - 1;
				var func = function(ob) {
					ob.start();
				};
				setTimeout(func, 1000, this);
			} else {
				this.fn();
			}
			return true;
		}
	},
	timer: function(details) {
		this.fn = details.fn ||
		function() {};
		if (details.time == 0) {
			this.time = 0;
		} else {
			this.time = details.time || 5;
		}
		this.message = details.message || false;
		this.script = details.script || false;
		this.stopping = false;
		this.stop = function() {
			this.stopping = true;
			return;
		};
		this.start = function() {
			if (Page.bcast(this.script) === false || this.stopping) {
				return;
			}
			this.time = parseInt(this.time, 0);
			if (this.message) {
				var html = this.message.replace('[time]', Utility.seconds_to_string(this.time));
				Console.status(this.script, html);
			}
			if (this.time > 0) {
				this.time = this.time - 1;
				var func = function(ob) {
					ob.start();
				};
				setTimeout(func, 1000, this);
			} else {
				this.fn();
			}
			return true;
		};
		return;
	},
	seconds_to_string: function(seconds) {
		if (seconds > 60) {
			var minutes = Math.floor(seconds / 60);
			seconds = seconds - (minutes * 60);
			if (minutes > 60) {
				var hours = Math.floor(minutes / 60);
				minutes = minutes - (hours * 60);
				return hours + 'h:' + minutes + 'm:' + seconds + 's';
			}
			return minutes + 'm:' + seconds + 's';
		}
		return seconds + 's';
	},
	int_dollars: function(num) {
		num = num || 0;
		var str = "";
		var tmp;
		while (num >= 1000) {
			tmp = num % 1000;
			if (tmp > 99) tmp = "" + tmp;
			else if (tmp > 9) tmp = "0" + tmp;
			else tmp = "00" + tmp;
			str = "," + tmp + str;
			num = Math.floor(num / 1000);
		}
		str = "$" + num + str;
		return str;
	},
	play_sound: function(src) {
		$('#sound_player').html( '<embed src="' + src + '" hidden="true" autostart="true" loop="false">' );
	},
	textarea_array: function(string) {
		if ( matches = string.match( new RegExp('.+', 'g') ) ) {
			if (matches.length > 0)
				return matches;
		}
		return [];
	}
};
#endif
